package com.leoandroid.tool.toolsbox.largeread.activity

import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.leoandroid.tool.toolsbox.largeread.BuildConfig
import com.leoandroid.tool.toolsbox.largeread.R
import com.leoandroid.tool.toolsbox.largeread.databinding.FragmentCardBinding
import com.leoandroid.tool.toolsbox.largeread.fragment.HomeFragment
import com.leoandroid.tool.toolsbox.largeread.fragment.MagnifierFragment
import com.leoandroid.tool.toolsbox.largeread.fragment.MyFragment
import com.leoandroid.tool.toolsbox.largeread.fragment.ToolFragment
import com.leoandroid.tool.toolsbox.largeread.utils.SPUtil
import com.leoandroid.tool.toolsbox.largeread.utils.ToolUtils
import com.leoandroid.tool.toolsbox.utils.AuditAdUtilsNew

class ToolsMainActivity : AppCompatActivity() {

    var bidingF: FragmentCardBinding? = null
    val binding get() = bidingF!!

    companion object {
        var AppVersionName = ""
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bidingF = FragmentCardBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ToolUtils.setStatusBarHomeTransparent(this)
//        StatusBarUtil.setLightMode(this)
        initView()
        AuditAdUtilsNew.openCSJSplashAd(
            this,
            BuildConfig.APPID,
            getString(R.string.app_name),
            BuildConfig.FISRT_SPLASH,
            binding.adContentViewSplash,
            object : AuditAdUtilsNew.onSplashAdListener {
                override fun splashEnd() {
                }
            })
    }

    private fun initView() {
        var fragmentList = arrayListOf<Fragment>()
        fragmentList.add(HomeFragment())
        fragmentList.add(ToolFragment())
        fragmentList.add(MyFragment())
        var adapter = FragmentAdapter(supportFragmentManager, fragmentList)
        binding.homeViewpager.adapter = adapter
        binding.homeViewpager.currentItem = 0
        setIconBtnState("home")
        binding.homeViewpager.setOnPageChangeListener(object : OnPageChangeListener{
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                when(position) {
                    0 -> {
                        setIconBtnState("home")
                    }
                    1 -> {
                        setIconBtnState("tool")
                    }
                    else -> {
                        setIconBtnState("setting")
                    }
                }
            }

            override fun onPageScrollStateChanged(state: Int) {
            }
        })

        binding.bottomBarHome.setOnClickListener {
            binding.homeViewpager.currentItem = 0
            setIconBtnState("home")
        }

        binding.bottomBarTool.setOnClickListener {
            binding.homeViewpager.currentItem = 1
            setIconBtnState("tool")
        }

        binding.bottomBarSetting.setOnClickListener {
            binding.homeViewpager.currentItem = 2
            setIconBtnState("setting")
        }
    }

    private fun setIconBtnState(type: String) {
        when(type){
            "home" -> {
                binding.bottomIcHome.setImageResource(R.mipmap.ic_home_enable)
                binding.bottomTextHome.setTextColor(resources.getColor(R.color.tools_title_color))
                binding.bottomIcTool.setImageResource(R.mipmap.ic_tool_unable)
                binding.bottomTextTool.setTextColor(resources.getColor(R.color.title_unable))
                binding.bottomIcSetting.setImageResource(R.mipmap.ic_setting_unable)
                binding.bottomTextSetting.setTextColor(resources.getColor(R.color.title_unable))
            }
            "tool" -> {
                binding.bottomIcHome.setImageResource(R.mipmap.ic_home_unable)
                binding.bottomTextHome.setTextColor(resources.getColor(R.color.title_unable))
                binding.bottomIcTool.setImageResource(R.mipmap.ic_tool_enable)
                binding.bottomTextTool.setTextColor(resources.getColor(R.color.tools_title_color))
                binding.bottomIcSetting.setImageResource(R.mipmap.ic_setting_unable)
                binding.bottomTextSetting.setTextColor(resources.getColor(R.color.title_unable))
            }
            else -> {
                binding.bottomIcHome.setImageResource(R.mipmap.ic_home_unable)
                binding.bottomTextHome.setTextColor(resources.getColor(R.color.title_unable))
                binding.bottomIcTool.setImageResource(R.mipmap.ic_tool_unable)
                binding.bottomTextTool.setTextColor(resources.getColor(R.color.title_unable))
                binding.bottomIcSetting.setImageResource(R.mipmap.ic_setting_enable)
                binding.bottomTextSetting.setTextColor(resources.getColor(R.color.tools_title_color))
            }
        }
    }


    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        when (requestCode) {
            1 -> if (ContextCompat.checkSelfPermission(
                    this,
                    Manifest.permission.CAMERA
                ) == PackageManager.PERMISSION_DENIED
            ) {
                //拒绝相机权限
                SPUtil.with(this).load().save("CAMERA", false)
                SPUtil.with(this).load().save("CAMERA_DENIED", true)
                Log.d(MagnifierFragment.TAG, "拒绝相机权限")
            } else {
                SPUtil.with(this).load().save("CAMERA", true)
                Log.d(MagnifierFragment.TAG, "同意相机权限")
            }
        }
    }

    class FragmentAdapter(fm: FragmentManager,var list: List<Fragment>):
        FragmentPagerAdapter(fm){

        override fun getCount(): Int {
            return list.size
        }

        override fun getItem(position: Int): Fragment {
            return list[position]
        }

    }
}